package cn.itcast.usermanage.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.MethodInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler;

import com.alibaba.druid.util.StringUtils;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@RequestMapping("user")
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping(value="users")
	public String toUsers(){
		return "users";		
	}
	
	@RequestMapping(value="list")
	@ResponseBody()
	public EasyUIResult queryUsersByPage(@RequestParam("page")Integer pageNum,@RequestParam("rows")Integer pageSize){
		return this.userService.queryEasyUiResult(pageNum,pageSize);
	}
	
	@RequestMapping(value="/page/{pageName}")
	public String toUserAdd(@PathVariable("pageName")String pageName){
		return pageName;
	}
	

	@RequestMapping(value="delete")
	@ResponseBody
	public Map<String, Object> deleteUsers(@RequestParam("ids")String[] ids){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			Boolean b = this.userService.deleteUsers(ids);
			if (b) {
				map.put("status", "200");
			} else {
				map.put("status", "500");
			} 
		} catch (Exception e) {
			map.put("status", "500");
			e.printStackTrace();
		}
		return map;
	}
	@RequestMapping(value="export/excel")
	public String exportExcel(@RequestParam("page")Integer pageNum, @RequestParam("rows")Integer pageSize, Model model){
		EasyUIResult easyUIResult = this.userService.queryEasyUiResult(pageNum, pageSize);
		model.addAttribute("userList", easyUIResult.getRows());
		return "userExcelView";
	}
	/**
	 * 根据ID查询用户
	 */
	@GetMapping(value="{id}")
	public ResponseEntity<User> queryUserById(@PathVariable("id")Long id){
		try {
			if (id.longValue() < 1) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			User user = this.userService.queryUserById(id);
			if (user == null) {
				//资源不存在404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.status(HttpStatus.OK).body(user);//200
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 新增用户
	 */
	@PostMapping()
	public ResponseEntity<Void>saveUser(User user){
		try {
			if (StringUtils.isEmpty(user.getUserName())) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.saveUser(user);
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 更新数据
	 */
	@PutMapping()
	public ResponseEntity<Void>updateUser(User user){
		try {
			if (user.getId() == null || user.getId() < 1) {
				//400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			this.userService.updateUser(user);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

}